package cn.hyy.hrm.controller;

import cn.hyy.basic.util.AjaxResult;
import cn.hyy.hrm.util.FastDfsApiOpr;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class FileController {

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){

        try {
            byte[] bytes = file.getBytes();//上传的文件内容

            //美女.png    extName="png"
            //文件名
            String filename = file.getOriginalFilename();
            //获取扩展名
            int index = filename.lastIndexOf(".");
            String extName = filename.substring(index+1);
            String fileId = FastDfsApiOpr.upload(bytes, extName);
            //要将fastdfs返回的file标识响应给前端
            return AjaxResult.me().setSuccess(true).setMessage("上传成功!").setResultObj(fileId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败!");
        }

    }

    /**
     * 文件删除
     * 参数： /group1/M00/00/00/rBEAC16C9IKAQ6E3AAI8-cLFXhc547.png
     */
    @GetMapping("/delete")
    public AjaxResult delete(String fileId){

        try {
            String groupName = null;//    group1
            String fileName = null;//     M00/00/00/rBEAC16C9IKAQ6E3AAI8-cLFXhc547.png

            fileId = fileId.substring(1);// fileId = group1/M00/00/00/rBEAC16C9IKAQ6E3AAI8-cLFXhc547.png
            int index = fileId.indexOf("/");
            groupName = fileId.substring(0,index);
            fileName = fileId.substring(index+1);

            FastDfsApiOpr.delete(groupName,fileName);
            return AjaxResult.me().setSuccess(true).setMessage("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    /**
     * 文件下载
     * @param fileId
     * @param response
     */
    @GetMapping(value = "/download")
    public void download(@RequestParam("fileId") String fileId, HttpServletResponse response){
        ResponseEntity<byte[]> result = null;
        InputStream inputStream = null;
        try {
            String groupName = null;//    group1
            String fileName = null;//     M00/00/00/rBEAC16C9IKAQ6E3AAI8-cLFXhc547.png

            fileId = fileId.substring(1);// fileId = group1/M00/00/00/rBEAC16C9IKAQ6E3AAI8-cLFXhc547.png
            int index = fileId.indexOf("/");
            groupName = fileId.substring(0,index);
            fileName = fileId.substring(index+1);
            //从fastdfs中下载文件，获取文件的字节数据
            byte[] bytes = FastDfsApiOpr.download(groupName,fileName);
            //将字节数据写入到响应对象的输出流中
            response.getOutputStream().write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
